package test.demos.ireport.utils;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class ReportFactory {
	private static final Logger log = LoggerFactory.getLogger(ReportFactory.class);
	public static final String PDF = "pdf";
	public static final String XLS = "xls";
	public static final String DOC = "doc";
	public static final String RTF = "rtf";
	public static final String DOT = ".";
	//public static final String SLASH = "/";
	public static final String SLASH = "\\";
	public static final String REPORT_BUNDLE = "config.report";
	public static final String PDF_STREAM = "application/pdf";
	public static final String EXCEL_STREAM = "application/vnd.ms-excel";
	public static final String WORD_STREAM = "application/vnd.ms-word";
	public static final String RTF_STREAM = "application/rtf";
	public static final String JRXML = "jrxml";
	public static final String JASPER = "jasper";
	public static final String MATERIAL_DATA_FABRIC_1 = "materialDatFabricList";
	// public static final String GF266 = "GF266";
	// public static final String REJECT_CASE_REPORT = "RejectCaseReport";
	// public static final String TRANSACTION_SUMMARY = "transactionSummary";
	// public static final String KIT_ITEM_LIST = "kitItemListReport";
	// public static final String MASTER_INVENTORY_LEDGER_REPORT =
	// "masterInventoryLedgerReport";
	// public static final String MASTER_INVENTORY_LEDGER_BREAKDOWN_REPORT =
	// "masterInventoryLedgerBreakDownReport";
	// public static final String MASTER_INVENTORY_LEDGER_BREAKDOWN_REPORT_EXCEL
	// = "masterInventoryLedgerBreakDownExcelReport";
	// public static final String ITEM_TEMPLATE_REPORT = "itemTemplateReport";
	// public static final String UI_TEMPLATE_REPORT = "uiTemplateReport";
	// public static final String STORES_ITEM_REPORT = "storesItemReport";
	// public static final String OVER_REPLACEMENT_REPORT =
	// "overReplacementReport";
	// public static final String OVER_REPLACEMENT_SUMMARY_REPORT =
	// "overReplacementSummaryReport";
	// public static final String DORMANT_STOCK_REPORT = "dormantStockReport";
	// public static final String BARCODE_DEMAND_FORM = "barcodeDemandForm";
	// public static final String REQUEST_ITEMS_REPORT = "requestItemsReport";
	// public static final String STORES_ITEM_REPORT_FORMATION =
	// "storeItemReportFormation";

	public static File generateReport(HttpServletRequest request, String type, String generateFileName, String format,
			List<Object> dataList) throws Exception {
		String fileName = "";
		File pdfFile = null;
		try {
			String jrxmlPath = request.getSession().getServletContext().getRealPath("/reports/masterDataList_fabric.jrxml"); 
			File reportFile = new File(jrxmlPath);
			String generatedFileDirectory = request.getSession().getServletContext().getRealPath("temp");
			fileName = generatedFileDirectory + SLASH + generateFileName;
		  
			CommonDataSource dataSource = new CommonDataSource(dataList);
			log.debug("reportFile="+reportFile+",fileName="+fileName); 
			JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
			log.debug("jasperDesign="+jasperDesign); 
			if (format != null && format.equalsIgnoreCase(PDF)) {
				String pdfFileName = fileName + DOT + PDF;
				ReportUtil.generatePDF(jasperDesign, dataSource, null, pdfFileName);
				pdfFile = new File(pdfFileName);
				// FileInputStream inputStream = new FileInputStream(pdfFile);
				// content = new DefaultStreamedContent(inputStream, PDF_STREAM,
				// generateFileName + DOT + PDF);
			}
		} catch (JRException jre) {
			jre.printStackTrace();
			log.info("[ReportFactory] JRException:" + jre.getMessage());
			throw jre;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[Exception]: ", e);
			log.info("[ReportFactory] Exception Message=" + e.getMessage());
			throw e;
		}

		return pdfFile;
	}

}
